package com.decent.ejfadmin.equity.cardproduct.dao.provider;


import com.decent.ejfadmin.equity.common.CardOperator;
import com.decent.ejfadmin.equity.common.CardProduct;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Objects;

/**
 * 产品
 *
 * @author weicx
 * @date 2019/03/16
 */
public class CardProductDaoProvider {


    private void generateAreaSql(StringBuilder sql, HashMap<String, Object> map) {
        CardProduct cardProduct = (CardProduct) map.get("cardProduct");
        if (cardProduct.getCardOperatorId() != null) {
            sql.append(" and p.card_operator_id = #{cardProduct.cardOperatorId} ");
        }
        if (StringUtils.isNotBlank(cardProduct.getCardProductName())) {
            sql.append(" and instr(p.card_product_name , #{cardProduct.cardProductName})>0 ");
        }
        if (cardProduct.getFace() != null) {
            sql.append(" and p.face = #{cardProduct.face} ");
        }
        if (StringUtils.isNotBlank(cardProduct.getProvince())) {
            sql.append(" and instr(p.province , #{cardProduct.province})>0  ");
        }
        if (cardProduct.getStatus() != null) {
            sql.append(" and p.status = #{cardProduct.status} ");
        }

    }

    public String queryCardOperatorCount(HashMap<String, Object> map) {
        StringBuilder sql = new StringBuilder(200);
        sql.append("SELECT\n" +
                "count(1)" +
                "FROM\n" +
                "yuhuang_card_operator o,yuhuang_card_type t WHERE o.card_type_id=t.card_type_id ");
        generateOperatorSql(sql, map);
        return sql.toString();
    }

    public String queryCardOperatorList(HashMap<String, Object> map) {
        StringBuilder sql = new StringBuilder(200);
        sql.append("SELECT\n" +
                "o.id,\n" +
                "o.card_type_id,\n" +
                "o.card_operator_id,\n" +
                "o.card_operator_name," +
                "o.xianyu_operator_name xianyuOperatorName,\n" +
                "o.logo_colours,\n" +
                "o.logo,\n" +
                "o.`status`,\n" +
                "o.gmt_create_time,\n" +
                "o.gmt_modified_time,\n" +
                "t.card_type_name,\n" +
                "o.remark," +
                "o.tipText," +
                "o.ticketTipText," +
                "o.spuid," +
                "o.originalPriceRecoveryStatus," +
                "o.spu_status spuStatus \n" +
                "FROM\n" +
                "yuhuang_card_operator o,\n" +
                "yuhuang_card_type t\n" +
                "WHERE o.card_type_id=t.card_type_id ");
        generateOperatorSql(sql, map);
        sql.append(" order by o.card_operator_id ");
        return sql.toString();
    }

    private void generateOperatorSql(StringBuilder sql, HashMap<String, Object> map) {
        CardOperator cardOperator = (CardOperator) map.get("cardOperator");
        if (cardOperator.getCardTypeId() != null) {
            sql.append(" and o.card_type_id = #{cardOperator.cardTypeId} ");
        }
        if (StringUtils.isNotBlank(cardOperator.getCardOperatorName())) {
            sql.append(" and instr(o.card_operator_name,#{cardOperator.cardOperatorName})>0 ");
        }
        if (Objects.nonNull(cardOperator.getCardOperatorId())) {
            sql.append(" and card_operator_id=#{cardOperator.cardOperatorId} ");
        }
        if (Objects.nonNull(cardOperator.getSpuid())) {
            sql.append(" and spuid=#{cardOperator.spuid} ");
        }
        if (Objects.nonNull(cardOperator.getSpuStatus())) {
            sql.append(" and spu_status=#{cardOperator.spuStatus} ");
        }
    }

}
